﻿/// <reference path="jsutil.js" />
var postHref = "/eNote/NoteMain/SaveNoteItem";
var loadingItem = "#loadingItem";

var gridDefs = [
{
    "ColumnDefinitions": [{ "Width": "108px" }, { "Width": "100%" }], "RowDefinitions": [{ "Height": "30px" }],
},
{
    "ColumnDefinitions": [{ "Width": "100%" }, { "Width": "108px" }], "RowDefinitions": [{ "Height": "30px" }]
}
];
$(function () {
    function installjXamlGrid() {
        $(".jXamlGrid0").jXamlGrid({
            gridDef: gridDefs[0]
        });
        $(".jXamlGrid1").jXamlGrid({
            gridDef: gridDefs[1]
        });
    }



    $(".resizable").resizable({});


    installNoteItemEditor();
    function installNoteItemEditor() {
        $('.NoteItemEditor').NoteItemEditor({
            postHref: postHref,
            ondelete: function (evt) {
                $('#noteItems').html(evt);
                installNoteItemEditor();
            },
            waitingItem: loadingItem
        });

        installjXamlGrid();
    }
    $('#addItem').click(function () {
        JsUtil.toggleShowHide(true, loadingItem);

        var note = { FName: $(this).attr("data-FName"), State: "Insert", Text: $("#newItemVal").attr("value") };
        $.ajax({
            url: postHref,
            type: "POST",
            data: JsUtil.AddAntiForgeryToken( JSON.stringify(note)),
            contentType: "application/json; charset=utf-8",
            fail: function () {
                JsUtil.toggleShowHide(false, loadingItem);
                alert('FAIL');
            },
            
            error: function (jqXHR, textStatus, errorThrown) {
                JsUtil.toggleShowHide(false, loadingItem);
                alert('ERROR');
                document.writeln('ERROR:'+"jqXHR:"+JSON.stringify( jqXHR)+"|textStatus:"+textStatus+"|errorThrown:"+errorThrown);                
            },
            success: function (evt) {
                JsUtil.toggleShowHide(false, loadingItem);
                $('#noteItems').html(evt);
                installNoteItemEditor();
                $("#newItemVal").attr("value", "");
            }
        });
    });


})